<?php

/**
 * Configure an Mz_Configurable object
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @package   Mz
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Configure.php 38 2009-02-18 17:12:39Z monzee $
 */
class Mz_Configure
{
    /**
     * Configures a configurable object
     * 
     * For each $key => $value pair of the array $config, the method 
     * setKey($value) of the configurable object is called. If the object
     * indicates that it has magic setters, it will be called regardless of the
     * existence of the setKey() method. The configurable object must capture
     * these calls and set its own state.
     *
     * @param Mz_Configurable   $obj    Object to configure
     * @param Zend_Config|array $config Options, either an array or a Zend_Config
     */
    static public function setConfig(Mz_Configurable $obj, $config)
    {
        if ($config instanceof Zend_Config) {
            $options = $config->toArray();
        } else if (!is_array($config)) {
            $options = (array) $config;
        }

        foreach ($options as $option => $value) {
            $setter = 'set' . ucfirst($option);
            if ($obj->hasMagicSetter() || is_callable(array($obj, $setter))) {
                call_user_func(array($obj, $setter), $value);
            }
        }
    }

}
